/*
 * @Author: wangjun haodreams@163.com
 * @Date: 2024-07-24 16:09:12
 * @LastEditors: wangjun haodreams@163.com
 * @LastEditTime: 2024-07-24 16:42:59
 * @FilePath: \godriver\opdb\example\node\main.go
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package main

import (
	"log"

	"gitee.com/haodreams/godriver/opdb"
	"gitee.com/haodreams/godriver/opdb/opsql"
	"gitee.com/haodreams/golib/logs"
	"gitee.com/haodreams/libs/ee"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

func main() {
	dataSource := "sis:openplant@10.202.10.192:8200?timeout=60"
	dialector := opsql.Open(dataSource)
	if dialector == nil {
		ee.NewError("不支持的驱动:" + dataSource).Once2(logs.CbErrorf)
		return
	}
	op, err := gorm.Open(dialector, &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
	})

	if err != nil {
		log.Fatalln(err)
	}

	node := new(opdb.Node)
	node.Name = "W3.XR2"
	node.Desc = "测试节点"
	err = InsertNodes(op, []*opdb.Node{node})
	if err != nil {
		log.Fatalln(err)
	}
}

/**
 * @description: 插入测点
 * @param {[]*opdb.Point} points
 * @return {*}
 */
func InsertNodes(op *gorm.DB, nodes []*opdb.Node) (err error) {
	//err := real.GetEngine().Exec("WRITE Point", needInsert...).Error
	if len(nodes) == 0 {
		return
	}
	needInsert := make([]interface{}, len(nodes))
	for i := range nodes {
		needInsert[i] = nodes[i]
	}
	db, err := op.DB()
	if err != nil {
		return
	}

	_, err = db.Exec("WRITE Node", needInsert...)

	return
}
